{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "<a href=\"https://colab.research.google.com/drive/1PA36flCkfyHWxiP4IJtZeJk-Bv24cKqN?usp=sharing\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
        "\n",
        "### 📊 Graph Prompting\n",
        "**Note:** This is a simplified version without usinga graph database\n",
        "\n",
        "Graph Prompting is an advanced method that uses graph structures to guide complex reasoning tasks.\n",
        "\n",
        "## Key points:\n",
        "\n",
        "* 🔑 **Core concept:** Represent problems as interconnected nodes in a graph, with prompts guiding traversal and reasoning.\n",
        "\n",
        "* 📊 **Structure:**\n",
        "  * Nodes represent concepts, sub-tasks, or decision points\n",
        "  * Edges represent relationships or transitions between nodes\n",
        "\n",
        "* ⚙️ **Process:**\n",
        "  * Define the problem as a graph\n",
        "  * Guide the model through the graph using targeted prompts\n",
        "  * Aggregate information from traversed nodes to form a solution\n",
        "\n",
        "* 🌟 **Advantages:**\n",
        "  * Handles complex, interconnected problems\n",
        "  * Allows for non-linear reasoning paths\n",
        "\n",
        "* 💼 **Applications:**\n",
        "  * Multi-step decision making\n",
        "  * Knowledge graph navigation\n",
        "  * Solving problems with multiple dependencies\n",
        "\n",
        "* 🚀 **Implementation:**\n",
        "  * Design the graph structure based on the problem domain\n",
        "  * Craft prompts for node exploration and edge traversal\n",
        "  * Develop strategies for information aggregation across nodes\n",
        "\n",
        "* ⚖️ **Challenges:**\n",
        "  * Designing effective graph structures\n",
        "  * Managing context across multiple graph traversals\n",
        "\n",
        "* 🔄 **Variations:**\n",
        "  * Dynamic graph prompting: Adjust the graph structure based on intermediate results\n",
        "  * Hierarchical graph prompting: Use nested graphs for multi-level reasoning"
      ],
      "metadata": {
        "id": "Y2eqHW0TK87s"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install -q -U langchain-groq==0.2.4"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "KI4D1HL-LKaP",
        "outputId": "a1b858de-2c74-457b-d283-50c09ba6d3f6"
      },
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[?25l   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/121.9 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m121.9/121.9 kB\u001b[0m \u001b[31m5.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain_groq import ChatGroq\n",
        "from langchain.prompts import ChatPromptTemplate"
      ],
      "metadata": {
        "id": "dfHHptAFLMwj"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "import getpass"
      ],
      "metadata": {
        "id": "eZJGDTIMLOnP"
      },
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 🔑 Provide Groq API Key\n",
        "\n",
        "- [Groq API Key](https://console.groq.com/keys)\n",
        "\n"
      ],
      "metadata": {
        "id": "rQrlkg_bLRZp"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "os.environ[\"GROQ_API_KEY\"] = getpass.getpass()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "XI8sLK8uLTht",
        "outputId": "487dfe3b-b8c6-47df-e057-f1e0ee61f326"
      },
      "execution_count": 4,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "··········\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "llm = ChatGroq(\n",
        "    model=\"llama3-8b-8192\",\n",
        "    temperature=0.5\n",
        ")"
      ],
      "metadata": {
        "id": "0os_AIehLVh1"
      },
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "graph_prompt = ChatPromptTemplate.from_messages(\n",
        "    [\n",
        "        (\"system\", \"You are an AI that can reason over graph-structured knowledge.\"),\n",
        "        (\n",
        "            \"human\", \"\"\"Given the following graph structure:\n",
        "                      Earth - neighboring planet -> Mars\n",
        "                      Mars - nickname -> Red Planet\n",
        "\n",
        "                      Answer the following question:\n",
        "                      {input}\n",
        "\n",
        "                      Answer:\"\"\"\n",
        "        ),\n",
        "    ]\n",
        ")\n",
        "\n",
        "chain = graph_prompt | llm"
      ],
      "metadata": {
        "id": "FToKQB7jLBYD"
      },
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "problem =  \"What is the nickname of the neighboring planet to Earth?\"\n",
        "response = chain.invoke({\"input\": problem})\n",
        "print(response.content)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0PWEvnMyLZSb",
        "outputId": "0e7106d2-1828-475f-8d2d-5dc780eb6634"
      },
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "I can reason over this graph structure!\n",
            "\n",
            "From the graph, I can see that the neighboring planet to Earth is Mars. And, I can also see that Mars has a nickname, which is \"Red Planet\".\n",
            "\n",
            "So, the answer to the question \"What is the nickname of the neighboring planet to Earth?\" is \"Red Planet\".\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "ja7ZCUY2MJT2"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}